package portal.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dao.PublicDao;
import dao.Query;
import dao.QueryCallBack;
import dao.QueryCallBackList;
import db.DBDruidManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import portal.beans.Gpu;
import portal.mybatis.dao.service.GpuService;
import portal.mybatis.page.Page;

/**
 * Created with IDEA
 * author:HMY
 * Date:2018/11/2
 * Time:19:44
 * //TODO
 */
@Service
public class ScheduleGpuDetailDao {
	@Autowired
	GpuService gpuService;
    public List<Gpu> getAllGpu(){
    	  return (List<Gpu>)gpuService.selectAllByOr(null, null, null);
    }

    public Gpu getGpuByVersion(String gpuVersion){
    	return gpuService.selectByVersion(gpuVersion);
    }

    public Gpu getGpuById(Integer gpuId){
    	System.out.println(gpuId);
    	if(gpuService==null) System.out.println("service is NULL");
    	return gpuService.selectGpuById(gpuId);
    }

    public boolean addGpu(Gpu gpu) {
       return gpuService.insertGpu(gpu);
    }

    public boolean updateGpu(Gpu gpu) {
    	return gpuService.updateGpu(gpu);
    }

    public boolean deleteGpu(int gpuId) {
    	return gpuService.deleteGpu(gpuId);
    }
    //Never Used ,has been replace by JobController中的方法。
    @SuppressWarnings("unchecked")
	public List<Object> getPagedGpuList(Integer onePageCount, Integer currentPage) {
    	Page<Gpu >p=gpuService.getPage(new Page<Gpu>(onePageCount,onePageCount*(currentPage-1),null,null));
    	return (List<Object>)p.getRows();
    }
    /**
     * By黄振 //TODO 分页查询GPU节点
     * @Param [onePageCount, currentPage]
     * limit 当前页是第几个记录，每页多少记录
     * @return java.util.List<java.lang.Object>
     **/
    @Deprecated
    public List<Object> getPagedGpuList1(Integer onePageCount, Integer currentPage) {
        String sql = "select * from schedule_gpu_detail order by create_time desc limit ?,?";
        List<Object> pagedGpuList = new ArrayList<>();
        pagedGpuList = Query.executeQueryList(pagedGpuList, sql
                , new Object[]{(currentPage - 1) * onePageCount, onePageCount}
                , new QueryCallBackList() {
                    @Override
                    public List<Object> doExecuteList(ResultSet rs, List<Object> results) {
                        try {
                            while (rs.next()) {
                                Gpu gpu = new Gpu();
                                try {
                                    gpu.setGpuIdentify(rs.getString("gpu_identify"));
                                    gpu.setGpuVersion(rs.getString("gpu_version"));
                                    gpu.setGpuDetail(rs.getString("gpu_detail"));
                                    gpu.setGpuId(rs.getInt("gpu_id"));
                                } catch (SQLException e) {
                                    e.printStackTrace();
                                }
                                results.add(gpu);
                            }
                        }catch (SQLException e){
                            e.printStackTrace();
                        }finally{
//                       	 DBDruidManager.closeConnection();
                        }
                        return results;
                    }
                });
        return  pagedGpuList;
    }

    /**
     * By黄振 //TODO 计算总页数
     * @Param [onePageCount] 每页的节点数
     * @return int
     **/
    public int getGpuPageNum(Integer onePageCount) {
        int gpuNum = this.getGpuNum();
        return (gpuNum % onePageCount) == 0 ? (gpuNum / onePageCount) : (gpuNum / onePageCount + 1);
    }
    /**
     * By黄振 //TODO 得GPU节点总数
     * @Param []
     * @return int
     **/
    public synchronized int getGpuNum() {
        String sql = "select count(*) from schedule_gpu_detail";
        return PublicDao.oneNum(sql,null);
    }
}

